package com.crr.controller;

import com.crr.service.FlowLimitService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA() {
        return "into ... A ...";
    }

    @GetMapping("/testB")
    public String testB() {
        return "into ... B ...";
    }

    /**
     * 流量控制链路演示
     * CD都访问flowLimitService.common() ，阈值对C限制，对D不管
     */
    @Resource
    FlowLimitService flowLimitService;

    @GetMapping("/testC")
    public String testC() {
        flowLimitService.common();
        return "into ...C ...";
    }

    @GetMapping("/testD")
    public String testD() {
        flowLimitService.common();
        return "into ... D ...";
    }

    /**
     * 排队等待
     * @return
     */
    @GetMapping("/testE")
    public String testE() {
        System.out.println(System.currentTimeMillis()+"---------------testE 排队等待");
        return "into ... E ...";
    }

    /**
     * 熔断规则-慢调用比例
     * @return
     */
    @GetMapping("/testF")
    public String testF() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-------------------测试，新增熔断规则-慢调用比例");
        return "-----------testF 新增熔断规则-慢调用比例";
    }
    /**
     * 熔断规则-异常比例
     * @return
     */
    @GetMapping("/testG")
    public String testG() {

        System.out.println("-------------------测试，新增熔断规则-异常比例");
        int num=10/0;
        return "-----------testF 新增熔断规则-异常比例";
    }
    /**
     * 熔断规则-异常数
     * @return
     */
    @GetMapping("/testH")
    public String testH() {

        System.out.println("-------------------测试，新增熔断规则-异常比例");
        int num=10/0;
        return "-----------testF 新增熔断规则-异常比例";
    }
}
